
<template>
  <scroll class="my-phone-list"
          ref="listview"
          @scroll="myScroll"
          :data="data"
          :probeType="3"
          :listenScroll="listenScroll">
    <ul>
      <li v-for="group in data"
          :key="group.id"
          ref="listGroup"
          class=list-group>
        <h2 class="list-group-title"
            ref="absorbHeight">{{group.title}}</h2>
        <ul>
          <li v-for="item in group.items"
              :key="item.name"
              @click="selectItem(item)"
              class="list-group-item">
            <img v-lazy="item.avatar"
                 class="avatar">
            <span class="name">{{item.name}}</span>
          </li>
        </ul>
      </li>
    </ul>

    <!-- 右侧字母列表 -->
    <div class="list-shortcut">
      <ul>
        <li v-for="(item, index) in shortcut"
            :key="item"
            @touchstart="onShortcutTouchStart"
            @touchmove.stop.prevent="onShortcutTouchMove"
            :data-index="index"
            :class="{ 'current': currentIndex === index }"
            class="item">{{ item }}</li>
      </ul>
    </div>
    <!-- 滚动固定标题实现 -->
    <div ref="fixed"
         v-show="fixedTitle"
         class="list-fixed">
      <h1 class="fixed-title">{{ fixedTitle }}</h1>
    </div>
    <!-- loading 组件 -->
    <div v-show="!data.length"
         class="loading-container">
      <my-loading></my-loading>
    </div>
  </scroll>
</template>
<script>

import Scroll from "base/scroll/scroll"
import { myDOM } from "common/js/dom"
import myLoading from 'base/loading/loading'
let ANCHOR_HEIGHT = 18;
let TITLE_HEIGHT = 30;
export default {
  created () {
    this.touch = {};
    this.listenScroll = true;
    this.listHeight = [];


  },
  data () {
    return {
      scrollY: -1,
      currentIndex: 0,
      diff: -1,
    }
  },
  props: {
    data: {
      type: Array,
      default: null
    }
  },
  computed: {
    // 快速入口排列数组
    shortcut () {
      return this.data.map((group) => {
        return group.title.substr(0, 1)
      })
    },
    fixedTitle () {
      if (this.scrollY > 0) {
        return ''
      }
      return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
    }
  },
  methods: {
    selectItem (item) {
      console.log(item);

      this.$emit('select', item);
    },
    onShortcutTouchStart (e) {
      let anchorIndex = myDOM.customAttribute(e.target, 'index');
      let firstTouch = e.touches[0];
      this.touch.y1 = firstTouch.pageY;
      this.touch.anchorIndex = Number(anchorIndex);
      this._scrollTo(anchorIndex);
    },
    onShortcutTouchMove (e) {
      let firstTouch = e.touches[0];
      this.touch.y2 = firstTouch.pageY;
      let delta = Math.floor((this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT || 0);

      let anchorIndex = Number(this.touch.anchorIndex) + delta;


      this._scrollTo(anchorIndex);


    },
    myScroll (pos) {
      this.scrollY = pos.y;
    },
    _scrollTo (index) {

      if (!index && index !== 0) { return }
      if (index < 0) {
        index = 0;
      }



      else if (index > this.listHeight.length - 2) {

        index = this.listHeight.length - 2;

      }


      this.currentIndex = Number(index);

      this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 0)
    }
    ,
    _calculateHeight () {
      this.listHeight = [];
      const list = this.$refs.listGroup;
      if (!list) return;
      let height = 0;
      this.listHeight.push(height);

      TITLE_HEIGHT = this.$refs.absorbHeight[0].clientHeight;
      for (let i = 0, len = list.length; i < len; i++) {
        let item = list[i];
        height += item.clientHeight;

        this.listHeight.push(height);

      }
    },

  },
  components: {
    Scroll,
    myLoading
  },
  watch: {
    data () {
      this.$nextTick(function () {
        this._calculateHeight();
      })
    },
    scrollY (newY) {
      let listHeight = this.listHeight;
      if (newY > 0) {
        this.currentIndex = 0;
        return
      }

      for (let i = 0, len = listHeight.length - 1; i < len; i++) {

        let height1 = listHeight[i];
        let height2 = listHeight[i + 1];
        if ((-newY >= height1 && -newY < height2)) {

          this.currentIndex = i;
          this.diff = height2 + newY;


          return
        }
      }
      this.currentIndex = 0;
    },
    diff (newVal) {
      let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0;
      this.$refs.fixed.style.transform = `translate3d(0,${fixedTop}px,0)`
    }

  },
  mounted () {
    this.$nextTick(function () {
      this._calculateHeight();
    })

  },

}
</script>

<style lang="scss">
@import '@~common/scss/mymixin.scss';
.my-phone-list {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: $color-background;
  .list-group {
    padding-bottom: 60px;
    .list-group-title {
      height: 60px;
      line-height: 60px;
      padding-left: 40px;
      @include font-size($font-size-small);
      color: $color-text-l;
      background: $color-highlight-background;
    }
    .list-group-item {
      display: flex;
      align-items: center;
      padding: 40px 0 0 60px;
      .avatar {
        width: 100px;
        height: 100px;
        border-radius: 50%;
      }
      .name {
        margin-left: 40px;
        color: $color-text-l;
        @include font-size($font-size-medium);
      }
    }
  }
  .list-shortcut {
    position: absolute;
    z-index: 30;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 40px;
    padding: 40px 0;
    border-radius: 20px;
    text-align: center;
    background: $color-background-d;
    font-family: Helvetica;
    .item {
      padding: 6px;
      line-height: 1;
      color: $color-text-l;
      @include font-size($font-size-medium);
      &.current {
        color: $color-theme;
      }
    }
  }
  .list-fixed {
    position: absolute;
    top: -1px;
    left: 0;
    width: 100%;
    .fixed-title {
      height: 60px;
      line-height: 60px;
      padding-left: 40px;
      @include font-size($font-size-small);
      color: $color-text-l;
      background: $color-highlight-background;
    }
  }
  .loading-container {
    position: absolute;
    width: 100%;
    top: 50%;
    transform: translateY(-50%);
  }
}
</style>
